package com.qfedu.controller;


import com.qfedu.pojo.SysUser;
import com.qfedu.pojo.User;
import com.qfedu.service.UserService;
import com.qfedu.vo.BaseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    private UserService userService;
//login
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
public BaseResult login(@RequestBody User user , HttpSession session){

        return userService.login(user,session);

    }

    //add
    @RequestMapping("/register")
    @ResponseBody
    public BaseResult register(@RequestBody User user , HttpSession session){

        System.out.println(user);
         return  userService.add(user,session);

    }
@RequestMapping("/deng")
@ResponseBody
    public BaseResult deng(HttpSession session){
        User user = (User) session.getAttribute("user");
        BaseResult baseResult=new BaseResult();

        if(user!=null){
            baseResult.setData(user);
            baseResult.setCode(200);
            return  baseResult;

        }else {
            baseResult.setCode(500);
            return  baseResult;
        }



}
    @RequestMapping("/update")
    @ResponseBody

    public BaseResult update(@RequestBody Map map, HttpSession session){
        String tel=map.get("tel").toString();
        return  userService.update(tel,session);


    }

@RequestMapping("/updateEmail")
@ResponseBody
    public BaseResult updateEmail(@RequestBody Map map, HttpSession session){
        String email=map.get("email").toString();
        return userService.updateEmail(email,session);
}

@RequestMapping("/updatePassword")
@ResponseBody
    public BaseResult updatePassword(@RequestBody Map map, HttpSession session){
        String password=map.get("password").toString();
        return userService.updatePassword(password,session);

}
    @RequestMapping("/updateuser")
    @ResponseBody
    public BaseResult updateUser(@RequestBody User user){


        return userService.updateUser(user);

    }
    @RequestMapping("/updateuser2")
    @ResponseBody
    public BaseResult updateUser2(@RequestBody User user, HttpServletRequest request){
        SysUser sysUser = new SysUser();
        sysUser.setId(user.getId());
        sysUser.setEmail(user.getEmail());
        sysUser.setPassword(user.getPassword());
   sysUser.setTel(user.getTel());
        request.getSession().removeAttribute("user");
         request.getSession().setAttribute("user",sysUser);

          return userService.updateUser(user);

    }


// 查
    @RequestMapping("/selectAll")
    @ResponseBody
    public ModelAndView selectAll( ) {

        List<User> sysUsers = this.userService.findAll();
        return  new ModelAndView("/admin/AllUser","users",sysUsers);

    }



// 删\

    @RequestMapping("/delUser")
    public String  delByTel(Integer id) {
        if (userService.delById(id)){
            return "redirect:../user/selectAll";
        }else{
            return null;
        }
    }
   // add
   @RequestMapping("/add")
   @ResponseBody
   public ModelAndView add(User user) {

       if (this.userService.insert(user)){
           return selectAll();
       }else {
           return null;
       }

   }
    @RequestMapping("/{path}")
    @ResponseBody
    public String goPage(@PathVariable String path) {
        return path;
    }

    @RequestMapping("/selectOne")
    @ResponseBody
    public ModelAndView selectOne(Integer id) {
        ModelAndView md=new ModelAndView();

        User user= userService.selectOne(id);
        md.addObject("user",user);
        md.setViewName("/admin/updateUser");
        return md;
    }


}
